package com.ksm.zyz.common.base;

import org.mybatis.spring.MyBatisSystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ailot on 2016/5/3.
 */
public class AppController<T> {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected List<T> list; // 公用list
    protected Map<String, Object> map = new HashMap<String, Object>();

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    @ExceptionHandler
    public ResponseEntity<String> exception(HttpServletRequest request, Exception e) {
        Enumeration enum1 = request.getHeaderNames();
        while (enum1.hasMoreElements()) {
            String key = (String) enum1.nextElement();
            String value = request.getHeader(key);
            logger.error("Head-->{}:{}", key, value);
        }
        logger.error("RequestURI:{}", request.getRequestURI());
        String erMsg = null;
        if (e instanceof DataAccessException) {
            e.printStackTrace();
            erMsg = "数据库异常";
            logger.error("数据库异常。。。{}", e.getMessage());
        } else if (e instanceof SQLException) {
            e.printStackTrace();
            erMsg = "SQL语法错误";
            logger.error("SQL语法错误。。。{}", e.getMessage());
        } else if (e instanceof NullPointerException) {
            e.printStackTrace();
            erMsg = "空指针异常";
            logger.error("空指针异常。。。{}", e.getMessage());
        } else if (e instanceof IOException) {
            e.printStackTrace();
            erMsg = "IO异常";
            logger.error("IO异常。。。{}", e.getMessage());
        } else if (e instanceof ClassNotFoundException) {
            e.printStackTrace();
            erMsg = "指定的类没找到";
            logger.error("指定的类没找到。。。{}", e.getMessage());
        } else if (e instanceof ArithmeticException) {
            e.printStackTrace();
            erMsg = "数学运算异常";
            logger.error("数学运算异常。。。{}", e.getMessage());
        } else if (e instanceof ArrayIndexOutOfBoundsException) {
            e.printStackTrace();
            erMsg = "数组越界";
            logger.error("数组越界。。。{}", e.getMessage());
        } else if (e instanceof IllegalArgumentException) {
            e.printStackTrace();
            erMsg = "方法参数错误";
            logger.error("方法参数错误。。。{}", e.getMessage());
        } else if (e instanceof IllegalStateException) {
            e.printStackTrace();
            erMsg = "在非法或不适当的时间调用方法时产生的信号";
            logger.error("在非法或不适当的时间调用方法时产生的信号。。。{}", e.getMessage());
        } else if (e instanceof MyBatisSystemException) {
            e.printStackTrace();
            erMsg = "mybatis异常";
            logger.error("mybatis异常。。。{}", e.getMessage());
        } else if (e instanceof ClassCastException) {
            e.printStackTrace();
            erMsg = "强制类型转换错误";
            logger.error("强制类型转换错误。。。{}", e.getMessage());
        } else {
            e.printStackTrace();
            erMsg = "系统错误,请联系管理员！";
            logger.error("系统错误。。。{}", e.getMessage());
        }
        return new ResponseEntity<String>(erMsg, HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
